const http = require("http");
const fs = require("fs");
const path = require("path");
http
  .createServer(function (req, res) {
    let str = "";
    console.log(req.url);
    if (req.url == "/") {
      str = `<p>首页模块</p>`;
    } else if (req.url == "/about") {
      str = `<p>关于模块</p>`;
    } else if (req.url === "/clock") {
      try {
        str = fs.readFileSync(
          path.join(__dirname, "files", "zks.html"),
          "utf-8"
        );
      } catch (error) {
        str = error;
      }
    } else {
      str = `<p>404</p>`;
    }
    res.statusCode = 200;
    // 如果要设置多个响应头，推荐用 setHeader()，然后再 writeHead()
    // 👉 如果只需要简单返回状态码和多个头部，直接用 writeHead()
    // res.writeHead(200, {
    //   "Content-Type": "text/html",
    //   "Custom-Header": "Hello",
    // });
    res.setHeader("Content-Type", "text/html;charset=utf-8");
    res.end(str);
  })
  .listen("1165");
